class Struct {
    constructor(key, value) {
        this.key = key;
        this.value = value;
    }
}

export default class Map {
    arr = []
    // set(key: K, value: V): this;
    set = (key, value) => {
        for (let i = 0; i < this.arr.length; i++) {
            if (this.arr[i].key === key) {
                this.arr[i].value = value
                return
            }
        }
        this.arr[this.arr.length] = new Struct(key, value);
        // this.arr.push = new Struct(key, value);
    }

    // get(key: K): V | undefined;
    get = (key) => {
        for (let i = 0; i < this.arr.length; i++) {
            if (this.arr[i].key === key) {
                return this.arr[i].value;
            }
        }
        return null;
    }

    // delete(key: K): boolean;
    delete = (key) => {
        let v;
        let isDelete = false
        for (let i = 0; i < this.arr.length; i++) {
            v = this.arr.pop();
            if (v.key === key) {
                continue;
            }
            this.arr.unshift(v);
            isDelete = true
        }
        return isDelete
    }
    // clear(): void;
    clear = (key) => {
        let v;
        for (let i = 0; i < this.arr.length; i++) {
            this.arr.unshift(v);
        }
        this.arr = []
    }

    // has(key: K): boolean;
    has = (key) => {
        let v;
        let isHave = false
        for (let i = 0; i < this.arr.length; i++) {
            v = this.arr.pop()
            if (v.key === key) {
                isHave = true
                continue;
            }
        }
        return isHave
    }

    //readonly size: number;
    size = () => {
        return this.arr.length;
    }

    // forEach(callbackfn: (value: V, key: K, map: Map<K, V>) => void, thisArg?: any): void;
    forEach = (callback = ()=>{}) => {
        for (let i = 0; i < this.arr.length; i++) {
            callback(this.arr[i].key, this.arr[i].value, i)
        }
    }
}